<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件


/**
 * 生成json字符串结果
 * @param  mixd  $msg    要生成的信息
 * @param  int   $code   结果码
 * @return  json
 */
if (!function_exists('json_result')) {
    function json_success($msg = 'success', $code = 0) {
        $res = array();
        if (is_array($msg) || is_object($msg)) {
            $res['result'] = $msg;
        } else {
            $res['msg'] = $msg;
        }
        $res['code']  = $code;

        echo json_encode($res);
    }
}
/**
 * curl以get方式请求
 * @param  str  $url  链接
 */
if (!function_exists('curl_get')) {
    function curl_get($url)
    {
        //初始化curl
        $ch = curl_init();
        //设置超时
        curl_setopt($ch, CURLOPT_TIMEOUT, 10);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        //运行curl
        $res = curl_exec($ch);
        curl_close($ch);
        return $res;
    }
}
/**
 * curl以post放上请求
 * @param str  $url  提交的地址
 * @param arr  $data 提交数据
 */
if (!function_exists('curl_post')) {
    function curl_post($url, $data) {
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $url);//设置链接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
        /*curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type:text/json;charset=utf-8',
            'Content-Length:' . strlen($data)
        ));//设置HTTP头*/
        curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//POST数据
        // curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);//连接前等待时间
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);// 设置执行超时时间
        $res = curl_exec($ch);//接收返回信息
        curl_close($ch); //关闭curl链接
        return $res;
    }
}